//
//  JSMeUserInfoVC.m
//  NiubilityProject
//
//  Created by Apple on 2017/11/24.
//  Copyright © 2017年 Apple. All rights reserved.
//

#import "JSMeUserInfoVC.h"
#import "JSPhotoView.h"
#import "userInfoTitleView.h"
#import "QMUIFloatLayoutView+JSHander.h"
#import "userDetaislInfoView.h"
#import "JSChangeInfoVC.h"
#import "JSCountryInfoModel.h"


@interface JSMeUserInfoVC ()
@property(nonatomic, weak) UIScrollView *scorellView;
@property(nonatomic, strong) YYAnimatedImageView *useriCon;
@property(nonatomic, weak)  YYLabel *userNameL;
@property(nonatomic, weak)  YYLabel *ageL;
@property(nonatomic, weak)  YYLabel *genderL;
@property(nonatomic, weak)  UILabel *descL;
@property(nonatomic, weak)  YYLabel *timeL;
@property(nonatomic, weak)  YYLabel *countryL;
@property(nonatomic, weak)  YYLabel *attentionL;
@property(nonatomic, weak)  YYLabel *fansL;
@property(nonatomic, weak) QMUIButton *phontBtn;
@property(nonatomic, weak) QMUIButton *userCenterBtn;
@property(nonatomic, weak) UIView *topContenView;
@property(nonatomic, strong) JSPhotoView *photoView; // 相册
@property(nonatomic, strong) userDetaislInfoView *userCenterView;
@property(nonatomic, weak) YYAnimatedImageView *bgImageV;
@property(nonatomic, assign) NSInteger currentSelectType;// 0:相册 1:个人信息
@property(nonatomic, strong) QMUIFloatLayoutView *floatLayoutView;
@property(nonatomic, weak) UILabel *userInfoL;
@property(nonatomic, strong)  UIVisualEffectView *visuaView;
@property(nonatomic, strong) UserInfo *userInfo;

@end

@implementation JSMeUserInfoVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    JS_WeakSelf(self)
    [RACObserve(userManager, photos) subscribeNext:^(id x){
        if (weakself.currentSelectType == 0) {
            weakself.photoView = nil;
            weakself.photoView.dataSource = x;
            weakself.photoView.alpha = 1;
        }
    }];
    [RACObserve(self, userInfo) subscribeNext:^(id x) {
        JSLog(@"%@", NSStringFromCGRect(weakself.timeL.frame));
    }];
    
}
- (void)updateViewConstraints{
    JS_WeakSelf(self)
    CGFloat margin10 = kRealValue(10);
    CGFloat space = kTopSpace;
    // score view
    [self.scorellView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(weakself.view);
    }];
    // top conten view
    [self.topContenView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(weakself.view);
        make.top.left.and.right.equalTo(weakself.scorellView);
    }];
    self.topContenView.backgroundColor = [UIColor orangeColor];
    // image
    [self.useriCon mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topContenView).offset(30 + space);
        make.centerX.equalTo(self.topContenView);
        make.width.mas_equalTo(kRealValue(55));
        make.height.equalTo(weakself.useriCon.mas_width).multipliedBy(1);
    }];
    // name
    [self.userNameL mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(self.topContenView);
        make.top.equalTo(weakself.useriCon.mas_bottom).offset(margin10);
        make.height.mas_equalTo(kRealValue(20));
    }];
    // age
    [self.ageL mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakself.userNameL.mas_bottom).offset(margin10);
        make.left.equalTo(self.topContenView);
        make.right.equalTo(weakself.useriCon.mas_left);
        make.height.mas_equalTo(kRealValue(20));
    }];
    // gender
    [self.genderL mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakself.ageL);
        make.right.equalTo(self.topContenView);
        make.left.equalTo(weakself.useriCon.mas_right);
        make.height.mas_equalTo(kRealValue(20));
        
    }];
    // desc
    [self.descL mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.topContenView).offset(10);
        make.right.equalTo(self.topContenView).offset(-10);
        make.top.equalTo(weakself.ageL.mas_bottom).offset(margin10);
        
    }];
    // time
    [self.timeL mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakself.ageL);
        make.top.equalTo(weakself.descL.mas_bottom).offset(margin10);
        make.height.mas_equalTo(kRealValue(20));
        
    }];
    // country
    [self.countryL mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakself.useriCon.mas_right).offset(-margin10);
        make.top.equalTo(weakself.timeL);
        make.height.mas_equalTo(kRealValue(20));
        
    }];
    
    // attention
    [self.attentionL mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakself.ageL);
//        make.left.equalTo(self.topContenView);
        make.top.equalTo(weakself.timeL.mas_bottom).offset(margin10);
    }];
    [self.fansL mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakself.attentionL);
        make.left.equalTo(weakself.genderL);
    }];
    // photo
    [self.phontBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.topContenView);
        make.width.equalTo(self.topContenView).multipliedBy(0.5);
        make.height.mas_equalTo(kRealValue(44));
        make.top.equalTo(weakself.attentionL.mas_bottom).offset(margin10);
    }];
    // user center
    [self.userCenterBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakself.phontBtn);
        make.right.equalTo(self.topContenView);
        make.width.equalTo(weakself.phontBtn).multipliedBy(1);
        make.height.equalTo(weakself.phontBtn).multipliedBy(1);
        make.bottom.equalTo(self.topContenView.mas_bottom);
    }];
    [self.bgImageV mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.and.left.and.right.equalTo(self.topContenView);
        make.bottom.equalTo(self.phontBtn.mas_top);
    }];
    //    RAC(self.bgImageV,image) = RACObserve(self.useriCon, image);
    
   
    [self.visuaView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.bgImageV);
    }];
    
#pragma mark --------------底部个人信息视图--------------------
    // bottom user info view
    if (self.currentSelectType) {
        self.userCenterView.hidden = NO;
        self.photoView.hidden = YES;
        [self.userCenterView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.topContenView.mas_bottom).offset(1);
            make.left.and.right.equalTo(self.topContenView);
//            make.bottom.equalTo(weakself.scorellView);
        }];
    }else{
        self.userCenterView.hidden = YES;
        self.photoView.hidden = NO;
        NSInteger a = userManager.photos.count % 3;
        NSInteger b = userManager.photos.count / 3;
        if (a) {
            b+=1;
        }
        CGFloat height = flat((CGRectGetWidth(self.view.bounds) - 2 *(kRealValue(10))) / 3) * b + (b+1) * (kRealValue(10));
        [self.photoView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.and.right.equalTo(weakself.scorellView);
            make.top.equalTo(weakself.topContenView.mas_bottom).offset(1);
            make.height.mas_equalTo(height);
//            make.bottom.equalTo(weakself.scorellView);
            
        }];
    }
    if (self.currentSelectType) {
        [self.scorellView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.userCenterView);
        }];
    }else{
        [self.scorellView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.photoView);
        }];
    }
    
#pragma mark --------------底部个人相册--------------------
    
   
    
    [super updateViewConstraints];
   
//    [self.scorellView mas_remakeConstraints:^(MASConstraintMaker *make) {
//        make.bottom.equalTo(weakself.photoView);
//    }];
}
- (void)didInitialized{
    [super didInitialized];
    self.view.backgroundColor = JS_colorWithHex(0xf7f7f7);
    _currentSelectType = 0;
    self.extendedLayoutIncludesOpaqueBars = NO;
    CGFloat space = kTopSpace;
   
    JS_WeakSelf(self)
    UIScrollView *scrollView = [UIScrollView new];
    scrollView.contentInset = UIEdgeInsetsMake(-64 - space, 0, 0, 0);    self.scorellView = scrollView;
    [self.view addSubview:scrollView];
    UIView *topContenView = [UIView new];
    self.topContenView = topContenView;
    topContenView.backgroundColor = [UIColor clearColor];
    [scrollView addSubview:topContenView];
    YYAnimatedImageView *useriCon = [YYAnimatedImageView new];
    self.useriCon = useriCon;    [topContenView addSubview:useriCon];
    YYLabel *userNameL = [YYLabel new];
    JSSetingLableInfo(userNameL, 14, JS_colorWithHex(kColorBlack3));
    userNameL.textAlignment = NSTextAlignmentCenter;    self.userNameL = userNameL;
    [topContenView addSubview:userNameL];
    YYLabel *ageL = [YYLabel new];
    ageL.textAlignment = NSTextAlignmentRight;
    self.ageL = ageL;
    JSSetingLableInfo(ageL, 13, JS_colorWithHex(kColorGray3));
    [topContenView addSubview:ageL];
    YYLabel *genderL = [YYLabel new];
    JSSetingLableInfo(genderL, 13, JS_colorWithHex(kColorGray3));
    self.genderL = genderL;    [topContenView addSubview:genderL];
    UILabel *descL = [UILabel new];
    descL.numberOfLines = 0;
    descL.textAlignment = NSTextAlignmentCenter;
    descL.lineBreakMode = NSLineBreakByCharWrapping;
    self.descL = descL;
    JSSetingLableInfo(descL, 13, JS_colorWithHex(kColorBlack3));
    [topContenView addSubview:descL];
    YYLabel *timeL = [YYLabel new];
    timeL.backgroundColor = JS_colorWithHex(kColorGray6);
    ViewRadius(timeL, 3);
    self.timeL = timeL;
    JSSetingLableInfo(timeL, 12, [UIColor whiteColor]);
    [topContenView addSubview:timeL];
    YYLabel *countryL = [YYLabel new];
    self.countryL = countryL;
    countryL.backgroundColor = JS_colorWithHex(kColorGray6);
    JSSetingLableInfo(countryL, 12, [UIColor whiteColor]);
    ViewRadius(countryL, 3);
    [topContenView addSubview:countryL];
    YYLabel *attentionL = [YYLabel new];
    attentionL.textAlignment = NSTextAlignmentRight;
    self.attentionL = attentionL;
    JSSetingLableInfo(attentionL, 12, [UIColor whiteColor]);
    [topContenView addSubview:attentionL];
    YYLabel *fansL = [YYLabel new];
    self.fansL = fansL;
    JSSetingLableInfo(fansL, 12, [UIColor whiteColor]);
    [topContenView addSubview:fansL];
    QMUIButton *photoBtn = [QMUIButton buttonWithType:UIButtonTypeCustom];
    photoBtn.tag = 1000;
    [photoBtn addTarget:self action:@selector(photoOrCenterClick:) forControlEvents:UIControlEventTouchUpInside];    photoBtn.selected = YES;
    photoBtn.imagePosition = QMUIButtonImagePositionLeft;
    self.phontBtn = photoBtn;    photoBtn.backgroundColor = [UIColor whiteColor];
    [photoBtn setTitleColor:JS_colorWithHex(kColorGray6) forState:UIControlStateNormal];
    [photoBtn setTitleColor:TabBarTintColor forState:UIControlStateSelected];
    photoBtn.titleLabel.font = UIFontMake(13);
    [photoBtn setTitle:JSLocalizedString(@"photo") forState:UIControlStateNormal];
    [photoBtn setImage:UIImageMake(@"me_photo") forState:UIControlStateNormal];
    [photoBtn setImage:UIImageMake(@"me_photo_select") forState:UIControlStateSelected];
    [topContenView addSubview:photoBtn];
    QMUIButton *userBtn = [QMUIButton buttonWithType:UIButtonTypeCustom];
    userBtn.tag = 1001;
    [userBtn addTarget:self action:@selector(photoOrCenterClick:) forControlEvents:UIControlEventTouchUpInside];    userBtn.imagePosition = QMUIButtonImagePositionLeft;
    self.userCenterBtn = userBtn;    userBtn.backgroundColor = [UIColor whiteColor];
    [userBtn setTitleColor:JS_colorWithHex(kColorGray6) forState:UIControlStateNormal];
    [userBtn setTitleColor:TabBarTintColor forState:UIControlStateSelected];
    userBtn.titleLabel.font = UIFontMake(13);
    [userBtn setTitle:JSLocalizedString(@"userCenter") forState:UIControlStateNormal];
    [userBtn setImage:UIImageMake(@"me_userCenter") forState:UIControlStateNormal];
    [userBtn setImage:UIImageMake(@"me_userCenter_select") forState:UIControlStateSelected];
    [topContenView addSubview:userBtn];
    // 毛玻璃效果
    YYAnimatedImageView *bgImageV = [YYAnimatedImageView new];
    [bgImageV setContentCompressionResistancePriority:UILayoutPriorityDefaultLow                                              forAxis:UILayoutConstraintAxisVertical];    self.bgImageV = bgImageV;
    [topContenView insertSubview:bgImageV atIndex:0];
    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    self.visuaView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
    [bgImageV addSubview:self.visuaView];
    userDetaislInfoView *bottomView = [userDetaislInfoView new];    //
    bottomView.hidden = YES;
    bottomView.backgroundColor = [UIColor whiteColor];
    [self.scorellView addSubview:bottomView];
    self.userCenterView = bottomView;
    JSPhotoView *photoView = [[JSPhotoView alloc] init];
    photoView.backgroundColor = JS_colorWithHex(0xf7f7f7);
    photoView.gridView.separatorColor = JS_colorWithHex(0xf7f7f7);
    [self.scorellView addSubview:photoView];
    self.photoView = photoView;
    
    [self resetUserInfo:userManager.curUserInfo];
    
}
#pragma mark ------------相册 or 个人中心点击----------------
- (void)photoOrCenterClick:(UIButton *)sender {
    if (sender.selected) {
        return;
    }
    self.currentSelectType = sender.tag - 1000;
    sender.selected = !sender.selected;
    if (sender.tag == 1000) {
        self.userCenterBtn.selected = NO;
    }else{
        self.phontBtn.selected = NO;
    }
    // 告诉self.view约束需要更新
    [self.view setNeedsUpdateConstraints];
    // 调用此方法告诉self.view检测是否需要更新约束，若需要则更新，下面添加动画效果才起作用
    [self.view updateConstraintsIfNeeded];
    [self.view layoutIfNeeded];
}

- (void)setNavigationItemsIsInEditMode:(BOOL)isInEditMode animated:(BOOL)animated{
    [super setNavigationItemsIsInEditMode:isInEditMode animated:animated];
    self.navigationItem.rightBarButtonItem = [QMUINavigationButton barButtonItemWithImage:UIImageMake(@"me_edit") position:QMUINavigationButtonPositionRight target:self action:@selector(editInfo:)];
}

-(UIImage *)navigationBarBackgroundImage{
    return [UIImage qmui_imageWithColor:[UIColor clearColor]];
}
#pragma mark ------------编辑个人信息----------------
- (void)editInfo:(UIButton *)sender {
    JSChangeInfoVC *vc = [[JSChangeInfoVC alloc] init];
    JS_VC_PUSHVC_ANIMATION(YES);
}
- (void) resetUserInfo:(UserInfo *)u{
    JS_WeakSelf(self)
    [self.useriCon setImageWithURL:[NSURL URLWithString:u.avatar] placeholder:defauldUserIcon options:YYWebImageOptionUseNSURLCache completion:^(UIImage * _Nullable image, NSURL * _Nonnull url, YYWebImageFromType from, YYWebImageStage stage, NSError * _Nullable error) {
        weakself.bgImageV.image = image;
        weakself.useriCon.image = [[image imageByResizeToSize:CGSizeMake(100, 100)] imageByRoundCornerRadius:50];
    }];
    self.userNameL.text = u.username;
    self.ageL.text = NSStringFormat(@"%@ %@",u.age,JSLocalizedString(@"YearsOld"));
    self.genderL.text = u.sex == UserGenderMale?JSLocalizedString(@"genderMan"):JSLocalizedString(@"genderWoman");
    
    self.descL.text = u.mood;
    self.timeL.text = u.before_time.length?NSStringFormat(@" %@ %@ ",u.before_time,JSLocalizedString(@"Minute")):NSStringFormat(@" %@ %@ ",@"",JSLocalizedString(@"Minute"));
    if (JSLocalizeIsChina()) {
        self.countryL.text = NSStringFormat(@" %@-%@ ",u.city.country_zh,u.city.state_zh);
    }else{
        self.countryL.text = NSStringFormat(@" %@-%@ ",u.city.country,u.city.state);
    }
    NSString *attentionStr = NSStringFormat(@"%@ %@",JSLocalizedString(@"attentionLoca"), u.star_count);
    self.attentionL.attributedText = [NSString seetingFormString:attentionStr subString:u.star_count defauldColor:[UIColor whiteColor] subColor:[UIColor blackColor]];
    NSString *fanStr =  NSStringFormat(@"%@ %@",JSLocalizedString(@"fansLoca"), u.fans_count);
    self.fansL.attributedText = [NSString seetingFormString:fanStr subString:u.fans_count defauldColor:[UIColor whiteColor] subColor:[UIColor blackColor]];
    self.photoView.dataSource = u.photo;
    [self.userCenterView setUserInfol:u];
    
    // 告诉self.view约束需要更新
    [self.view setNeedsUpdateConstraints];
    // 调用此方法告诉self.view检测是否需要更新约束，若需要则更新，下面添加动画效果才起作用
    [self.view updateConstraintsIfNeeded];
    [self.view layoutIfNeeded];
}
@end
